package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     * @param dto
     */
    @Override
    public void addCart(ShoppingCartDTO dto) {
        // 创建ShoppingCart对象
        ShoppingCart shoppingCart = new ShoppingCart();
        // 拷贝属性值
        BeanUtils.copyProperties(dto, shoppingCart);

        // 1.判断当前菜品或套餐是否在购物车中--条件：dishId+dishFlavor+userId
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.getBy(shoppingCart);
        if (cart == null) { // 购物车中没有此菜品或套餐
            // 2.补充缺失的属性值
            // 判断是新增菜品还是新增套餐
            if (dto.getDishId() != null){   //代表新增的是菜品
                // 根据菜品id查询菜品表，获取菜品相关信息
                Dish dish = dishMapper.getById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else {                         //代表新增的是套餐
                // 根据套餐id查询套餐数据
                Setmeal setmeal = setmealMapper.getBySetmealId(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 3.将商品数据存入到shopping_cart表中
            shoppingCartMapper.insert(shoppingCart);
        }else { //购物车中有该商品
            // 4.将原来的购物车商品数量加1，调用Mapper更新方法
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.update(cart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.list(userId);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车一个商品
     * @param dto
     */
    @Override
    public void sub(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(dto, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        ShoppingCart cart = shoppingCartMapper.getBy(shoppingCart);
        if (cart != null){
            if (cart.getNumber() == 1){
                shoppingCartMapper.deleteById(cart.getId());
            }else {
                cart.setNumber(cart.getNumber() - 1);
                shoppingCartMapper.update(cart);
            }
        }
    }
}
